import { defineStore } from 'pinia'
import { ref } from 'vue'
import {loginApi} from '@/api/user.js'

export const useUserStore = defineStore(
    'user',
    () => {
        const token = ref('')
        const userInfo = ref({ name: 'admin' })

        // 登录操作
        function login(data) {

            loginApi(data).then()
            token.value = new Date().getTime()
            userInfo.value = {
                name: 'admin'
            }
            return Promise.resolve()
        }

        // 退出登录
        function loginOut() {
            // loginOutApi()
            //     .then((res) => {})
            //     .catch((error) => {})
            //     .finally(() => {
            //         localStorage.removeItem('tabs')
            //         localStorage.removeItem('vuex')
            //         sessionStorage.removeItem('vuex')
            //         location.reload()
            //     })
            token.value = ''
            location.reload()
        }
        return { token, userInfo, login, loginOut }
    },
    {
        persist: true
    }
)
